#coding:utf-8

from flask import Flask, request
import apiBussiness,apiGrequests
import json,datetime
import appconf


app = appconf.app
#
# @app.route('/refresh')
# def back_runJob():
#     httpApi.apiPlanRun.back_runJob()
#     return json.dumps({'errorCode': ''})


@app.route('/project/srhname/byId')
def project_srhname_byId():
    idList = request.args.get("idList")
    return json.dumps({'errorCode': '', 'data': apiBussiness.project_srh_name(idList)})

@app.route('/moudle/all/select')
def moudle_all_select():
    project_id = request.args.get("project_id")
    moudle_type = request.args.get("moudle_type")
    return json.dumps({'errorCode': '', 'data': apiBussiness.moudle_select_all(project_id,moudle_type)})

@app.route('/project/all/select')
def project_all_select():
    return json.dumps({'errorCode': '', 'data': apiBussiness.project_select_all()})

@app.route('/moudle/srh')
def moudle_srh():
    project_id = request.args.get("project_id")
    moudle_type = request.args.get("moudle_type")
    page = request.args.get("page")
    limit = request.args.get("limit")
    items = apiBussiness.moudle_srh(project_id,moudle_type,page,limit)
    return json.dumps({'errorCode': '', 'data': items[0],'count':items[1]})

@app.route('/moudle/manage', methods=['POST'])
def moudle_manage():
    content = request.json
    id = apiBussiness.moudle_manage(content)
    return json.dumps({'errorCode': '', 'id': id})

@app.route('/project/all')
def project_all():
    page = request.args.get("page")
    limit = request.args.get("limit")
    items = apiBussiness.project_all(page,limit)
    return json.dumps({'errorCode': '', 'data': items[0],'count':items[1]})

@app.route('/project/manage', methods=['POST'])
def project_manage():
    content = request.json
    id = apiBussiness.project_manage(content)
    return json.dumps({'errorCode': '', 'id': id})


@app.route('/case/api/params/srh')
def case_api_params_srh():
    project_id = request.args.get("project_id")
    param_name = request.args.get("param_name")
    page = request.args.get("page")
    limit = request.args.get("limit")
    items = apiBussiness.case_api_params_srh(project_id, param_name, page, limit)
    return json.dumps({'errorCode': '', 'data': items[0],'count':items[1]})


@app.route('/case/api/params/manage', methods=['POST'])
def case_api_params_manage():
    content = request.json
    id = apiBussiness.case_api_params_manage(content)
    return json.dumps({'errorCode': '', 'id': id})

@app.route('/case/api/params/del', methods=['POST'])
def case_api_params_del():
    content = request.json
    id = apiBussiness.case_api_params_del(content['id'])
    return json.dumps({'errorCode': '', 'id': id})

@app.route('/case/api/manage', methods=['POST'])
def case_api_manage():
    content = request.json

    id = apiBussiness.case_api_manage(content)
    return json.dumps({'errorCode': '', 'id': id})

@app.route('/case/api/del', methods=['POST'])
def case_api_del():
    """
    装载测试参数信息
    :return:
    """
    content = request.json
    return json.dumps({'errorCode': '', 'id': apiBussiness.case_api_del(content['id'])})
@app.route('/case/api/srh')
def case_api_srh():
    project_id = request.args.get("project_id")
    module_id = request.args.get("module_id")
    effect = request.args.get("effect")
    path = request.args.get("path")
    page = request.args.get("page")
    limit = request.args.get("limit")
    items = apiBussiness.case_api_srh(project_id,module_id,effect,path,page,limit)
    return json.dumps({'errorCode': '', 'data': items[0],'count':items[1]})

@app.route('/case/api/plan/srh')
def case_api_plan_srh():
    project_id = request.args.get("project_id")
    module_id = request.args.get("module_id")
    api_plan_name = request.args.get("api_plan_name")
    page = request.args.get("page")
    limit = request.args.get("limit")
    items = apiBussiness.case_api_plan_srh(project_id,module_id,api_plan_name,page,limit)
    return json.dumps({'errorCode': '', 'data': items[0], 'count': items[1]})

@app.route('/case/api/plan/manage', methods=['POST'])
def case_api_plan_manage():
    """
    装载测试参数信息
    :return:
    """
    content = request.json
    return json.dumps({'errorCode': '', 'id': apiBussiness.case_api_plan_manage(content)})
#

@app.route('/case/api/plan/del', methods=['POST'])
def case_api_plan_del():
    """
    装载测试参数信息
    :return:
    """
    content = request.json
    return json.dumps({'errorCode': '', 'id': apiBussiness.case_api_plan_del(content['id'],content['isRun'])})


@app.route('/case/api/result/srh')
def case_api_result_srh():
    project_id = request.args.get("project_id")
    module_id = request.args.get("module_id")
    api_plan_id = request.args.get("api_plan_id")
    api_result = request.args.get("api_result")
    path = request.args.get("path")
    page = request.args.get("page")
    limit = request.args.get("limit")
    items = apiBussiness.case_api_result_srh(project_id, module_id, api_plan_id, api_result, path, page, limit)
    return json.dumps({'errorCode': '', 'data': items[0], 'count': items[1]})

@app.route('/case/api/result/time/history')
def case_api_result_time_history():
    path = request.args.get("path")
    items = apiBussiness.case_result_rspTime(path)
    return json.dumps({'errorCode': '', 'timelist': items[0], 'dataList': items[1]})

@app.route('/case/api/params/name')
def case_api_params_name():
    project_id = request.args.get("project_id")
    params_name = request.args.get("params_name")
    return json.dumps({"errorCode":'','data':apiBussiness.case_api_params_srhByName(project_id,params_name)})

@app.route('/case/api/run/byId')
def case_api_run_byId():
    caseId = request.args.get("caseId")
    responseList = apiGrequests.runApi(caseId)
    if responseList == False:
        return json.dumps({"errorCode": '', 'data': "未执行：header、body需要为json模式", 'nowtime': str(datetime.datetime.now())})
    elif type(responseList) == str:
        return json.dumps(
            {"errorCode": '', 'data': "执行失败：%s"%(responseList), 'nowtime': str(datetime.datetime.now())})
    else:
        avg = {"min":min(responseList[2]),"max":max(responseList[2]),"avg":sum(responseList[2])/(len(responseList[2])*1.0)}
        tres ={"True":len([x for x in responseList[1] if x==True]), "False":len([x for x in responseList[1] if x==False])}
        return json.dumps({"errorCode":'',"resultCount":tres,"avg":avg,'data':responseList[0],'nowtime':str(datetime.datetime.now())})
